package com.ruoyi.oar.service.impl;

import java.util.List;

import com.ruoyi.oar.controller.vo.OarImpNetHousePartField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.oar.mapper.OarImpNetHouseMapper;
import com.ruoyi.oar.domain.OarImpNetHouse;
import com.ruoyi.oar.domain.dto.OarImpNetHouseDTO;
import com.ruoyi.oar.domain.query.OarImpNetHouseQuery;
import com.ruoyi.oar.service.OarImpNetHouseService;

import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 导入的网约房Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-24
 */
@Service
public class OarImpNetHouseServiceImpl implements OarImpNetHouseService {
    @Autowired
    private OarImpNetHouseMapper oarImpNetHouseMapper;

    /**
     * 查询导入的网约房
     *
     * @param id 导入的网约房主键
     * @return 导入的网约房
     */
    @Override
    public Optional<OarImpNetHouseDTO> selectOarImpNetHouseById(String id) {
        return oarImpNetHouseMapper.selectOarImpNetHouseById(id).map(oarImpNetHouse -> toDto(oarImpNetHouse, true));
    }

    /**
     * 查询导入的网约房列表
     *
     * @param oarImpNetHouseQuery 导入的网约房
     * @return 导入的网约房
     */
    @Override
    public List<OarImpNetHouseDTO> selectOarImpNetHouseList(OarImpNetHouseQuery oarImpNetHouseQuery) {
        return oarImpNetHouseMapper.selectOarImpNetHouseList(oarImpNetHouseQuery).stream().map(this::toDto).collect(Collectors.toList());
    }

    @Override
    public List<OarImpNetHouseDTO> selListByTaskId(String taskId) {
        return oarImpNetHouseMapper.selectOarImpNetHouseByTaskId(taskId).stream().map(this::toDto).collect(Collectors.toList());
    }

    /**
     * 导出部分字段Excel
     * @param oarImpNetHouseQuery
     * @return
     */
    @Override
    public List<OarImpNetHousePartField> selectOarImpNetHousePartList(OarImpNetHouseQuery oarImpNetHouseQuery) {
        return oarImpNetHouseMapper.selectOarImpNetHouseList(oarImpNetHouseQuery).stream().map(oarImpNetHouse -> {
            OarImpNetHousePartField dto = BeanUtil.toBean(oarImpNetHouse, OarImpNetHousePartField.class);
            return dto;
        }).collect(Collectors.toList());
    }

    /**
     * 分页查询导入的网约房列表
     *
     * @param oarImpNetHouseQuery 导入的网约房
     * @return
     */
    @Override
    public TableDataInfo<OarImpNetHouseDTO> findAll(OarImpNetHouseQuery oarImpNetHouseQuery) {
        List<OarImpNetHouse> list = oarImpNetHouseMapper.selectOarImpNetHouseList(oarImpNetHouseQuery);
        List<OarImpNetHouseDTO> dtoList = list.stream().map(this::toDto).collect(Collectors.toList());
        return PageUtil.getDataTable(dtoList, new PageInfo(list).getTotal());
    }

    /**
     * 新增导入的网约房
     *
     * @param oarImpNetHouseDTO 导入的网约房
     * @return 结果
     */
    @Override
    public int insertOarImpNetHouse(OarImpNetHouseDTO oarImpNetHouseDTO) {
        checkOarImpNetHouse(oarImpNetHouseDTO);
        return oarImpNetHouseMapper.insertOarImpNetHouse(toEntity(oarImpNetHouseDTO));
    }

    /**
     * 修改导入的网约房
     *
     * @param oarImpNetHouseDTO 导入的网约房
     * @return 结果
     */
    @Override
    public int updateOarImpNetHouse(OarImpNetHouseDTO oarImpNetHouseDTO) {
        checkOarImpNetHouse(oarImpNetHouseDTO);
        return oarImpNetHouseMapper.updateOarImpNetHouse(toEntity(oarImpNetHouseDTO));
    }

    /**
     * 批量删除导入的网约房
     *
     * @param ids 需要删除的导入的网约房主键
     * @return 结果
     */
    @Override
    public int deleteOarImpNetHouseByIds(String[] ids) {
        return oarImpNetHouseMapper.deleteOarImpNetHouseByIds(ids);
    }

    /**
     * 删除导入的网约房信息
     *
     * @param id 导入的网约房主键
     * @return 结果
     */
    @Override
    public int deleteOarImpNetHouseById(String id) {
        return oarImpNetHouseMapper.deleteOarImpNetHouseById(id);
    }

    /**
     * 批量插入
     *
     * @param houseList
     * @param batchName
     * @return
     */
    @Override
    public int batchImpNetHouse(List<OarImpNetHousePartField> houseList, String batchName) {
        List<OarImpNetHouse> list = houseList.stream().map(oarImpNetHouse -> {
            return BeanUtil.toBean(oarImpNetHouse, OarImpNetHouse.class).setBatchName(batchName).setIsCheck(false).setIsIssuedCheck(false);
        }).collect(Collectors.toList());
        return oarImpNetHouseMapper.batchImpNetHouse(list);
    }


    /**
     * 批量更新是否下发状态
     *
     * @param oarImpNetHouseDTO
     * @return
     */
    @Override
    public int updateOarImpNetHouseByIds(OarImpNetHouseDTO oarImpNetHouseDTO) {
        return oarImpNetHouseMapper.updateOarImpNetHouseByIds(toEntity(oarImpNetHouseDTO));
    }

    /**
     * 修改临时网约房为下发
     *
     * @param oarImpNetHouseDTO
     * @return
     */
    @Override
    public int updateisIssuedCheckByCreateBy(OarImpNetHouseDTO oarImpNetHouseDTO) {
        return oarImpNetHouseMapper.updateisIssuedCheckByCreateBy(toEntity(oarImpNetHouseDTO));
    }


    /**
     * 根据批次号查询临时网约房列表
     *
     * @param taskId
     * @return
     */
    @Override
    public TableDataInfo<OarImpNetHouseDTO> selectOarImpNetHouseByTaskId(String taskId) {
        List<OarImpNetHouse> list = oarImpNetHouseMapper.selectOarImpNetHouseByTaskId(taskId);
        List<OarImpNetHouseDTO> dtoList = list.stream().map(this::toDto).collect(Collectors.toList());
        return PageUtil.getDataTable(dtoList, new PageInfo(list).getTotal());
    }

    /**
     * 待核查网约房数量
     * @param createBy
     * @param isCheck
     */
    @Override
    public Integer countByIsCheck(String createBy,String isCheck) {
        return oarImpNetHouseMapper.countByIsCheck(createBy,isCheck);
    }


    /**
     * 批量更新
     * @param houseList
     * @return
     */
    @Override
    @Transactional
    public void batchUpdate(List<OarImpNetHouseDTO> houseList) {
        houseList.forEach(oarImpNetHouseDTO->{
            oarImpNetHouseMapper.updateOarImpNetHouse(toEntity(oarImpNetHouseDTO));
        });
    }


    @Override
    public List<OarImpNetHouseDTO> selectListByTaskId(String taskId) {
        List<OarImpNetHouse> list = oarImpNetHouseMapper.selectOarImpNetHouseByTaskId(taskId);
        return list.stream().map(this::toDto).collect(Collectors.toList());
    }


    /**
     * 校验
     *
     * @param dto
     */
    private void checkOarImpNetHouse(OarImpNetHouseDTO dto) {

    }

    private OarImpNetHouse toEntity(OarImpNetHouseDTO dto) {
        return BeanUtil.toBean(dto, OarImpNetHouse.class);
    }


    private OarImpNetHouseDTO toDto(OarImpNetHouse entity) {
        return toDto(entity, false);
    }


    /**
     * 转DTO
     *
     * @param entity
     * @param withDetail 是否查询其他信息
     * @return
     */
    private OarImpNetHouseDTO toDto(OarImpNetHouse entity, Boolean withDetail) {
        OarImpNetHouseDTO dto = BeanUtil.toBean(entity, OarImpNetHouseDTO.class);
        if (withDetail) {

        }
        return dto;
    }
}
